<?php
/**
 * 描述
 * Created by Paco
 * Date: 2018/11/15 Time: 11:40
 */

namespace App\Repositories\Product;


use App\Lib\Utils\Category;
use App\Models\Product\Img;
use App\Models\Product\ProAttribue;
use App\Models\Product\Product;
use App\Models\Product\ProModel;
use App\Models\Product\Sku;
use App\Models\Shop;
use App\Repositories\Repository;
use Illuminate\Support\Facades\DB;

class ProductRepository extends Repository
{

    public function product($params)
    {
        $products = Product::query()->orderBy('created_at', 'desc')
            ->where('is_del', 0)
            ->when(!empty($params['shop_id']), function ($query) use ($params) {
                $query->where('shop_id', $params['shop_id']);
            })->when(!empty($params['cat_id']), function ($query) use ($params) {
                $query->where('cat_id', $params['cat_id']);
            })->when(!empty($params['product_name']), function ($query) use ($params) {
                $query->where('product_name', 'like', '%' . $params['product_name'] . '%');
            })->paginate($this->page_size, ['*'], 'page', $this->page);
        $shop_ids = [];
        foreach ($products as $k => $item) {
            $shop_ids[] = $item->shop_id;
        }
        $shop_ids = array_unique($shop_ids);
        $shops = $this->shops($shop_ids);
        foreach ($products as $k => $item) {
            $products[$k]->cat_id = $this->nameIds($item->cat_id);
            $products[$k]->shop_id = $shops[$item->shop_id];
        }

        return $this->respondWith($products);
    }

    public function show($pro_id)
    {
        $product = Product::query()->where('product_id', $pro_id)->first();
        if (!$product) {
            $this->throwRespondError(10109);
        }

        $imgs = Img::query()->where('product_id', $pro_id)->get();
        $attrs = ProAttribue::query()->where('product_id', $pro_id)->get();
        $skus = Sku::query()->where('product_id', $pro_id)
            ->where('status', 0)
            ->get();
        $data = [
            'product' => $product,
            'imgs' => $imgs,
            'attrs' => $attrs,
            'skus' => $skus
        ];

        return $this->respondWith($data);
    }


    public function forbid_sale($pro_ids)
    {
        if (!is_array($pro_ids)) {
            $pro_ids = explode(',', $pro_ids);
        }
        $user = $this->getUser();
        try {
            Product::query()->where('shop_id', $user->shop_id)
                ->whereIn('product_id', $pro_ids)
                ->update(['status' => 1]);
            return $this->respondSuccess();
        } catch (\Exception $e) {
            return $this->respondError(10108);
        }
    }

    public function checked($pro_ids, $checked, $reason)
    {

        try {
            $product = Product::query()->where('product_id', $pro_ids)->first();
            $product->checked = $checked;
            if ($reason) {
                $product->check_reason = '<div>' . $reason . '</div>' . $product->check_reason;
            }
            $product->save();
            return $this->respondSuccess();
        } catch (\Exception $e) {
            $this->throwRespondWithMessage($e->getMessage());
            return $this->respondError(10108);
        }
    }


    private function nameIds($cat_id)
    {
        $data = [];
        $redis_key = config('rediskey.product.category');
        $lib_cat = new Category();
        $categorys = app('phpredis')->get($redis_key);
        if ($categorys) {
            $categorys = json_decode($categorys, true);
            $categorys = $lib_cat->setArrayKey($categorys);
            if (key_exists($cat_id, $categorys)) {
                $cat = $categorys[$cat_id];
                $ids = explode('-', $cat['path']);
                foreach ($ids as $id) {
                    if (key_exists($id, $categorys)) {
                        $data[] = $categorys[$id]['name'];
                    }
                }
                $data[] = $categorys[$cat_id]['name'];
            }
        } else {

            $categorys = \App\Models\Product\Category::query()->orderBy('sort', 'desc')->get()->toArray();
            if ($categorys) {
                $categorys = $lib_cat->getTree($categorys);
                app('phpredis')->set($redis_key, json_encode($categorys));
                $this->nameIds($cat_id);
            }
        }
        return implode('-', $data);
    }

    private function shops($shop_ids)
    {
        $data = [];
        $shops = Shop::query()->whereIn('shop_id', $shop_ids)->get()->toArray();
        foreach ($shops as $shop) {
            $data[$shop['shop_id']] = $shop['shop_name'];
        }
        return $data;
    }

}